package com.example.study.plus.mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.study.plus.mybatis_plus.entity.Friend;
import com.example.study.plus.mybatis_plus.entity.User;
import com.example.study.plus.mybatis_plus.mapper.FriendMapper;
import com.example.study.plus.mybatis_plus.service.FriendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.study.plus.mybatis_plus.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 好友关联表 服务实现类
 * </p>
 *
 * @author 路飞4
 * @since 2023-08-18
 */
@Service
@Slf4j
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {
    @Resource
    @Lazy
    UserService userService;

    @Override
    public int applyForFriends(Long fromUserID,Long toUserId) {
        User from = userService.getUserByUserId2(fromUserID);
        User to = userService.getUserByUserId2(toUserId);
        if (null == to) {
            return 201;
        }
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fromId", from.getId());
        queryWrapper.eq("acceptId", to.getId());
        Friend friend1 = baseMapper.selectOne(queryWrapper);
        if (null != friend1) {
            if (friend1.getStatus() == 200) {
                return 202;//已经是好友
            } else {
                return 203;//已经发送过
            }
        }
        QueryWrapper<Friend> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("fromId", to.getId());
        queryWrapper2.eq("acceptId", from.getId());
        Friend friend2 = baseMapper.selectOne(queryWrapper2);
        if (null != friend2) {
            if (friend2.getStatus() == 200) {
                return 202;//已经是好友
            } else {
                return 205;//对方发送过申请
            }
        }
        Friend friend = new Friend();
        friend.setFromId(from.getId());
        friend.setAcceptId(to.getId());
        return baseMapper.insert(friend);
    }

    @Override
    public List<Friend> queryTheListOfApplications(Long userID) {
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 100);
        queryWrapper.eq("fromId", userID);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Friend> enquireAboutReceivingApplications(Long userID) {
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 100);
        queryWrapper.eq("acceptId", userID);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public int agreeToApply(Long myID, Long fromID) {
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("acceptId", myID);
        queryWrapper.eq("fromId", fromID);
        Friend friend = baseMapper.selectOne(queryWrapper);
        friend.setStatus(200);
        return baseMapper.update(friend, queryWrapper);
    }

    @Override
    public List<User> queryYourFriendsList(Long myID) {
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("acceptId", myID).eq("status", 200);
//        queryWrapper.or().eq("fromId", myID).eq("status", 200);


        List<Friend> friendList = baseMapper.selectList(queryWrapper);
        log.info("好友列表"+friendList.toString());
        List<User> userList = new ArrayList<>();
        for (Friend friend : friendList) {
            User user;
            if (!friend.getFromId().equals(myID)) {
                user = userService.getUserByUserId2(friend.getFromId());
            } else {
                user = userService.getUserByUserId2(friend.getAcceptId());
            }
//            if (!Objects.equals(user.getId(), myID)){
                userList.add(user);
//            }
            log.info("好友列表"+user);
        }

        QueryWrapper<Friend> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("fromId", myID).eq("status", 200);


        friendList = baseMapper.selectList(queryWrapper2);
        log.info(friendList.toString());
        for (Friend friend: friendList) {
            User user;
            if (friend.getAcceptId().equals(myID)) {
                user = userService.getUserByUserId2(friend.getFromId());
            } else {
                user = userService.getUserByUserId2(friend.getAcceptId());
            }
//            if (!Objects.equals(user.getId(), myID)){
                userList.add(user);
//            }
        }
        log.info("好友列表3"+userList);
        return userList;
    }

    @Override
    public int deleteAFriend(Long myID, Long fromID) {
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 200);
        queryWrapper.eq("fromId", myID)
                .eq("acceptId", fromID)
                .or()
                .eq("fromId", fromID)
                .eq("acceptId", myID);
        return baseMapper.delete(queryWrapper);
    }
}
